package com.wyp.didishige.activity;

import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;

import com.baidu.mapapi.model.LatLng;
import com.baidu.mapapi.utils.DistanceUtil;
import com.wyp.didishige.R;
import com.wyp.didishige.adapter.DiscoverAdapter;
import com.wyp.didishige.model.Dynamic;
import com.wyp.didishige.model.User;
import com.wyp.didishige.wiget.xlist.XListView;

import java.util.ArrayList;
import java.util.List;

import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.datatype.BmobGeoPoint;
import cn.bmob.v3.listener.FindListener;

public class GirlDynamicActivity extends BaseActivity implements XListView.IXListViewListener {

    XListView user_activities;

    User user;
    DiscoverAdapter nearByAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_female);
        user = (User) getIntent().getSerializableExtra("user");
        actionBar = getSupportActionBar();
        actionBar.setDisplayHomeAsUpEnabled(true);
        actionBar.setTitle(user.getNick());
        initView();
    }

    private void initView() {
        user_activities = (XListView) findViewById(R.id.user_activities);

        user_activities.setPullLoadEnable(true);
        user_activities.setPullRefreshEnable(false);
        user_activities.setXListViewListener(this);

        nearByAdapter = new DiscoverAdapter(this, new ArrayList<Dynamic>());
        user_activities.setAdapter(nearByAdapter);

        user_activities.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Bundle bundle;
                bundle = new Bundle();
                bundle.putString("from", "male");
                bundle.putString("username", user.getUsername());
//                IntentUtil.start_activity(GirlDynamicActivity.this, GirlActivity.class, bundle);
            }
        });
        user_activities.startLoadMore();
    }

    @Override
    public void onRefresh() {

    }

    int page = 0;

    private void loadData() {
        BmobQuery<Dynamic> query = new BmobQuery<Dynamic>();
        query.addWhereEqualTo("shiMei", user.getUsername());
        query.include("user,shiGeObject");
        query.order("-createdAt");

        query.setSkip(page * 20);
        query.setLimit(20);
        query.findObjects(this, new FindListener<Dynamic>() {
            @Override
            public void onSuccess(List<Dynamic> requestItems) {
                if (requestItems != null && requestItems.size() != 0) {
                    LatLng ll;
                    User cuUser = userManager.getCurrentUser(User.class);
                    for (Dynamic info : requestItems) {
                        BmobGeoPoint point = info.getLocation();
                        if (point == null) continue;
                        ll = new LatLng(point.getLatitude(), point.getLongitude());
                        int meter = (int) DistanceUtil.getDistance(ll, new LatLng(cuUser.getLocation().getLatitude(), cuUser.getLocation().getLongitude()));
                        info.setMeter(meter);
                    }

                    nearByAdapter.addAll(requestItems);
                    page++;
                } else {
                    user_activities.setPullLoadEnable(false);
                }
            }

            @Override
            public void onError(int i, String s) {

            }

            @Override
            public void onFinish() {
                super.onFinish();
                user_activities.stopLoadMore();
            }
        });
    }

    @Override
    public void onLoadMore() {
        loadData();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
//        getMenuInflater().inflate(R.menu.menu_female, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();

        if (id == android.R.id.home) {
            finish();
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}
